package com.winit.openApi.web;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.base.bean.ExecuteResult;
import com.winit.commons.util.HttpClientUtil;
import com.winit.openApi.manager.OpenApiManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

/**
 * Created by wanglin on 2018/6/7.
 */
@Component
@Api(value = "openApi", tags = {"openApi"}, description = "openApi")
@RestController
@RequestMapping("/api/v1/openApi")
public class OpenApiController {
    @Autowired
    OpenApiManager openApiManager;
    @Value("${openApiUrl}")
    String openApiUrl;

    @RequestMapping(value = "/initopenApi", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "刷取openApi数据")
    @ResponseBody
    public void initOpenApi(){
        openApiManager.initOpenApi();
    }



    @RequestMapping(value = "/openApiHttp", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "openApiHttp")
    @ResponseBody
    public ExecuteResult openApiHttp(@RequestBody JSONObject jsonObject){
        ExecuteResult er=new ExecuteResult();
        try {
            JSONObject json = JSONObject.parseObject(HttpClientUtil.doPost(openApiUrl, jsonObject.toJSONString()));
            er.setData(json);
        } catch (Exception e) {
            er.setCode("1");
            er.setDesc(e.getMessage());
            return er;
        }
        return er;
    }
}
